# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

import logging

from absl import (app, flags)
import pandas
import glob

FLAGS = flags.FLAGS


def calc_usd_value_total(filepath):
  df = pandas.read_csv(filepath, sep=',', header=0)
  try:
    usd_value = df['usd_value'].sum()
    return usd_value
  except KeyError:
    return 0


def main(_):
  root_dir = FLAGS.root_dir
  assert root_dir is not None

  usd_value = 0
  for filepath in glob.iglob(root_dir + '**/*.csv', recursive=True):
    usd_value += calc_usd_value_total(filepath)

  print(usd_value)


if __name__ == '__main__':
  flags.DEFINE_string('root_dir', None, 'csvs root dir.')

  logging.basicConfig(level='DEBUG', format='%(levelname)8s %(asctime)s %(name)s] %(message)s')
  app.run(main)
